const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);

let socketsList = [];

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

io.on('connection', async(socket) => {
    socketsList.push(socket);
    const ids = await io.allSockets();
    console.log('a user connected', ids);
    socket.on('disconnect', async() => {
        const ids = await io.allSockets();
        console.log('user disconnected', ids);
    });
    socket.on('chat message', (msg) => {
        console.log('message: ' + msg);
    });
});

server.listen(3000, () => {
    console.log('listening on *:3000');
});